package View;

import Controller.Main;

import javax.swing.*;
import java.awt.*;

/**
 * Panel to hold all of the JLabels for the tiles. Held by the MapFrame.
 */
public class MapPanel extends JPanel
{
    static final int screenWidth = 128;
    static final int screenHeight =72;
    static final int labelSize = 10;

    JLabel[][] tiles = new JLabel[screenWidth][screenHeight];
    public MapPanel()
    {
        this.setLayout(null);
        this.setBounds(new Rectangle(0, 0, screenWidth * 10, screenHeight * 10));
        this.initialize();
        JLabel back = new JLabel();
        back.setBounds(0,0,1280,720);
        back.setBackground(Color.BLACK);
        this.add(back);
        this.setVisible(true);
    }

    /**
     * Initializes an array of JLabels for the display.
     */
    public void initialize()
    {
        for(int i = 0; i < screenWidth; i++)
        {
            for(int j = 0; j< screenHeight; j++)
            {
                tiles[i][j] = new JLabel();
                tiles[i][j].setOpaque(true);
                this.tiles[i][j].setBounds(i*labelSize,j*labelSize,labelSize,labelSize);
                this.add(tiles[i][j]);
            }
        }
    }

    public void drawTile(int x, int y, Color c)
    {
        this.tiles[x][y].setBackground(c);
        repaint();
    }

    /**
     * Takes in an array of colors and draws them on the JLabels on the screen
     * @param colorArray array of colors for the tiles on the screen.
     */
    public void drawMap(Color[][] colorArray)
    {
        for(int i = 0; i < screenWidth; i++)
        {
            for(int j = 0; j < screenHeight; j++)
            {
                //System.out.println(colorArray[i][j].getRed()+" "+colorArray[i][j].getGreen()+ " "+ colorArray[i][j].getBlue()+" "+ colorArray[i][j].getAlpha());
                this.tiles[i][j].setBackground(colorArray[i][j]);
                this.tiles[i][j].setForeground(colorArray[i][j]);
            }
        }
    }

    /**
     * Method to test drawing the map, using ImageIcons. REALLY slow, one reason why I'm NOT using these icons no more
     */
    public void testMap()
    {
        for(int i = 0; i < screenWidth; i++)
        {
            for(int j = 0; j < screenHeight; j++)
            {
                this.tiles[i][j].setIcon(new ImageIcon(MapPanel.class.getResource("/Sprites/Tiles/FOREST.png")));
                this.tiles[i][j].setBounds(i*labelSize,j*labelSize,labelSize,labelSize);
            }
        }
    }
}
